#include "wiki_data_table.h"
#include "sqlite_manager.h"
#include "easylogging++.h"

WikiDataTable::WikiDataTable()
{

}

bool WikiDataTable::insertWikiData(QString uuid, QString data)
{
    SqliteManager sm;
    bool ret = sm.insertRowData("wiki_data",
    {
        "uuid", "wiki_data"
    },
    {
        uuid, data
    });
    if (!ret)
    {
        LOG(ERROR) << "insert row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiDataTable::insertWikiDataInTransaction(QString uuid, QString data, SqliteManager &sm)
{
    bool ret = sm.insertRowDataInTransaction("wiki_data",
    {
        "uuid", "wiki_data"
    },
    {
        uuid, data
    });
    if (!ret)
    {
        LOG(ERROR) << "insert row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiDataTable::deleteWikiDataByUuidInTransaction(QString uuid, SqliteManager &sm)
{
    bool ret = sm.deleteData("wiki_data",
    {
        {"uuid", uuid}
    });

    if (!ret)
    {
        LOG(ERROR) << "delete row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiDataTable::selectWikiDataByUuid(QString uuid, QString &wiki_data)
{
    SqliteManager sm;
    QList<QVariantList> data;
    bool ret = sm.selectData("wiki_data", {"wiki_data"}, {{"uuid", uuid}}, data);
    if (!ret)
    {
        LOG(ERROR) << "select wiki data failed, error info: "
                   << sm.lastError().toStdString();
    }

    wiki_data = data[0][0].toString();

    return ret;
}

bool WikiDataTable::updateWikiData(QString uuid, QString data)
{
    SqliteManager sm;
    bool ret = sm.updateData("wiki_data",
                             {
                                 {"wiki_data", data}
                             },
                             {
                                 {"uuid", uuid}
                             });
    if (!ret)
    {
        LOG(ERROR) << "update wiki data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}
